package com.dabai.qrtools.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import com.dabai.qrtools.BuildConfig;
import com.dabai.qrtools.R;
import com.dabai.qrtools.data.InitSmallProgram;
import com.dabai.qrtools.dialog.MdcDialog;
import com.dabai.qrtools.other.MainUtils;
import com.dabai.qrtools.qrcode.QRCodeUtils;
import com.dabai.qrtools.utils.DabaiUtils;
import com.dabai.qrtools.utils.FileUtils;
import com.dabai.qrtools.utils.StringUtils;
import com.dabai.qrtools.utils.ThemeUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.textfield.TextInputLayout;
import com.tencent.bugly.beta.Beta;

import java.util.Objects;
import java.util.Random;

public class SettingsActivity extends AppCompatActivity {

    private MaterialToolbar mToolbar;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        initViews();
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }

        context = this;

        ThemeUtils.setAutoUiModeStatusBar(this, this);
        setSupportActionBar(mToolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void initViews() {
        mToolbar = findViewById(R.id.toolBar);
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {

        Context context;

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);

            context = getContext();

            //版本号初始化
            final Preference ver = getPreferenceManager().findPreference("version");
            if (ver != null) {
                ver.setSummary(BuildConfig.VERSION_NAME + "(" + BuildConfig.VERSION_CODE + ")");
            }

        }


        @Override
        public boolean onPreferenceTreeClick(Preference preference) {

            switch (preference.getKey()) {

                case "scanSound":
                    boolean scanSound = preference.getSharedPreferences().getBoolean("scanSound", false);
                    if (scanSound) {
                        QRCodeUtils.playSound(context);
                    }
                    break;
                case "scanVibration":
                    boolean scanVibration = preference.getSharedPreferences().getBoolean("scanVibration", true);
                    if (scanVibration) {
                        QRCodeUtils.playVibration(context);
                    }
                    break;
                case "share":
                    InitSmallProgram.shareApp(context);
                    break;
                case "feedback":
                    DabaiUtils.openLinkWithBrowserActivity(context, "https://support.qq.com/product/134211");
                    break;
                case "version":
                    Beta.checkUpgrade();
                    break;
                case "sideLength":

                    MdcDialog.showInputDialog(context, "二维码尺寸", DabaiUtils.getSharedPreferencesString(context, "sideLength", "500")
                            , "请输入二维码图片边长(px)", new MdcDialog.OnInputDialogButtonListener() {
                                @Override
                                public void confirm(DialogInterface dialog, String content) {
                                    if (StringUtils.allIsDigital(content)) {

                                        int sideLength;

                                        try {
                                            sideLength = Integer.parseInt(content);
                                        } catch (NumberFormatException e) {
                                            DabaiUtils.showToast(context, "保存失败:数值异常");
                                            return;
                                        }

                                        if (sideLength > 4000) {
                                            DabaiUtils.showToast(context, "保存失败:边长不能超过4000px");
                                            return;
                                        }

                                        DabaiUtils.setSharedPreferencesString(context, "sideLength", content);
                                        dialog.dismiss();

                                    } else {
                                        DabaiUtils.showToast(context, "保存失败:数值异常");
                                    }
                                }

                                @Override
                                public void start(DialogInterface dialog, TextInputLayout textInputLayout) {
                                    textInputLayout.getEditText().setFilters(
                                            new InputFilter[]{
                                                    new InputFilter.LengthFilter(4)
                                            });
                                    textInputLayout.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);

                                    textInputLayout.setCounterEnabled(true);
                                    textInputLayout.setCounterMaxLength(4);

                                }

                                @Override
                                public void cancel() {

                                }
                            }).getWindow().setWindowAnimations(R.style.DBAnim_Transitions_IOS);


                    break;
                case "versionSet":

                    MdcDialog.showSeekbarDialog(context, "设置二维码版本", 40, MainUtils.getQrVersion(context), new MdcDialog.OnSeekbarDialogButtonListener() {
                        @Override
                        public void confirm(DialogInterface dialog, int progress) {
                            DabaiUtils.getSettingSharedPreferences(context).edit().putInt("version", progress).apply();
                            dialog.dismiss();
                        }
                        @Override
                        public void cancel() {

                        }
                    }).getWindow().setWindowAnimations(R.style.DBAnim_Transitions_IOS);

                    break;
            }
            return super.onPreferenceTreeClick(preference);
        }


    }
}